package com.mp.approvalreq.servlet;

import com.alibaba.fastjson.JSONObject;
import com.mp.approvalreq.entity.Query;
import com.mp.approvalreq.entity.RelateHistory;
import com.mp.approvalreq.service.RequestServicePrx;
import com.mp.user.entity.User;
import com.plusmoney.util.ParameterUtils;
import com.qq.tars.client.util.ServantUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/getRelateHistory")
public class GetRelateHistory extends AbstractServlet {

    private static final Logger logger = LoggerFactory.getLogger(GetRelateHistory.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            User user = getReqUser(req);
            Query query = new Query();
            query.setRequestId(ParameterUtils.getIntegerParameter(req, "requestId"));
            query.setCompanyId(user.getCompanyId());

            RequestServicePrx requestServicePrx = ServantUtils.getObject(RequestServicePrx.class);
            List<RelateHistory> relateHistoryList = requestServicePrx.getRelateHistory(query);

            returnResp(resp, relateHistoryList);
        } catch (Throwable throwable) {
            onThrowable(req, resp, throwable);
        }
    }

    private void returnResp(HttpServletResponse resp, List list) throws IOException {
        JSONObject json = new JSONObject();
        json.put("erpCode", "000");
        json.put("erpMsg", "成功");
        json.put("erpData", list);

        logger.info(json.toJSONString());

        printJson(resp, json.toJSONString());
    }
}
